<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Auth;
use think\Config;
class Dynamic extends Auth{
	// 公司动态列表
	public function dynamicList(){
		$list = Db::name('dynamic')->paginate(10);
		$this->assign('list',$list);
		return $this->fetch();
	}
	// 添加公司动态
	public function addDynamic(){
		return $this->fetch();
	}
	// 执行添加公司动态
	public function doAddDynamic(){
		$arr = ['title','introduce','content'];
		$data = input('post.');
		$result_param = $this->check_param($arr,$data);
		if(!$result_param){
			$this->error('请补充完整');
			exit;
		}
		$file = request()->file('image');
		if(!empty($file)){
			$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
			if($info){
				// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
				$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
			}else{
				// 上传失败获取错误信息
				$this->error($file->getError());
				exit;
			}
		}else{
			$this->error('请选择图片');
			exit;
		}
		$data['createtime'] = time();
		$data['is_top'] = 0;
		$result = Db::name('dynamic')->insert($data);
		if($result){
			$this->redirect('/admin/dynamic/addDynamic');
		}else{
			$this->error('添加失败');
		}
	}
	// 动态置顶
	public function dynamicTop(){
		$did = input('did');
		if(empty($did)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$did);
		$data['is_top'] = 1;
		$where_two['id'] = array('NEQ',$did);
		$data_two['is_top'] = 0;
		Db::startTrans();
		try{
			Db::name('dynamic')->where($where)->update($data);
			Db::table('dynamic')->where($where_two)->update($data_two);
			// 提交事务
			Db::commit();
			$this->redirect('/admin/dynamic/dynamicList');
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			$this->error('设置失败');
		}
	}
	// 动态详情
	public function dynamicInfo(){
		$id = trim(input('did'));
		if(empty($id)){
			$this->error('禁止非法操作');
			exit;
		}
		$info = Db::name('dynamic')->where('id = '.$id)->find();
		if(empty($info)){
			$this->error('该动态不存在');
			exit;
		}
		$this->assign('info',$info);
		return $this->fetch();
	}
	// 修改动态
	public function updateDynamic(){
		$did = input('post.did');
		if(empty($did)){
			$this->error('禁止非法操作');
			exit;
		}else{
			$where['id'] = array('EQ',$did);
		}
		$title = trim(input('post.title'));
		$introduce = trim(input('post.introduce'));
		$content = trim(input('post.content'));
		if(!empty($title)){
			$data['title'] = $title;
		}
		if(!empty($introduce)){
			$data['introduce'] = $introduce;
		}
		if(!empty($content)){
			$data['content'] = $content;
		}
		$file = request()->file('image');
		if(!empty($file)){
			$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
			if($info){
				// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
				$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
			}else{
				// 上传失败获取错误信息
				$this->error($file->getError());
				exit;
			}
		}
		$result = Db::name('dynamic')->where($where)->update($data);
		if($result){
			$this->redirect('/admin/dynamic/dynamicList');
		}else{
			$this->error('修改失败');
		}
	}
}